package com.soft.mainsite.common.utils;


import com.alibaba.fastjson.JSONArray;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: SmsUtil
 *
 * @author Jin
 * @create 2017-07-27
 */
public class SmsUtil {

    private static final Logger log = LoggerFactory.getLogger(SmsUtil.class);

    private static final String URL = "https://api.netease.im/sms/sendtemplate.action";
    private static final String APP_KEY = "cc58c823717404e2bb009fb055f659cf";
    private static final String APP_SECRET = "e4a1120b04c5";
    private static final String NONCE = "12345";

    public static void sendTemplateMsg(String mobile, String templateId, String... params) {

        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumUtil.getCheckSum(APP_SECRET, NONCE, curTime);

        JSONArray paramArray = new JSONArray();
        for (String param : params) {
            paramArray.add(param);
        }
        Map<String, String> queryMap = new HashMap<>(3);
        queryMap.put("templateid", templateId);
        queryMap.put("mobiles", "[\"" + mobile + "\"]");
        queryMap.put("params", paramArray.toJSONString());

        HttpResponse response = HttpRequest
                .post(URL)
                .header("charset", "utf-8")
                .header("contentType", "application/x-www-form-urlencoded")
                .header("AppKey", APP_KEY)
                .header("Nonce", NONCE)
                .header("CurTime", curTime)
                .header("CheckSum", checkSum)
                .query(queryMap)
                .send();
        log.info(response.bodyText());
    }

}
